require('./filter');
var _ = require('lodash');
var Router = require('koa-router');
var service = require('./service');
var config = require('../../config');
var Service = require('../page/service');

var router = module.exports = new Router();

router.get('/page', function*(next) {
  this.body = this.render('templates/index.html');
});
router.post('/ajax/list',function*(next){
  var self = this;
  var data = yield Service.dataList();
  // console.log(data);
  this.body = this.render(200, data);
})
router.post('/ajax/deleteData',function *(next){
  var form = this.request.body;
  var self=this;
  console.log(form)
  var data=yield Service.deleteData(form.route);
  this.body=this.render(200,data);
})
router.get('/list', function*(next) {
  this.body = this.render('templates/list.html');
});
router.all('/api/:route', function*(next) {
  var self=this;
console.log(self.params)
var param=self.params.route;
  var data=yield Service.saveData(param);
  console.log(data);
  var jsonData=data[0]['jsonData'];
  console.log(JSON.parse(jsonData));
  var data= this.render(200,jsonData).data.message.replace(/&#39;/g,'\'');
  // console.log(111)
  // console.log(data)
  // console.log(data.indexOf('code'))
  var data=JSON.parse(data);
  console.log(data);
  
  if(data.code){
    this.body=this.render(200,data).data;
  }
  else{
    this.body=this.render(200,data);
  }
 
})

router.get('/page/:route', function*(next) {
  var self=this;
  var param=self.params;
  console.log(param);
  var data=yield Service.saveData(param.route);
  console.log(data[0])
  this.body = this.render('templates/index.html',{
    data:data,
    id:param.route,
    portName:data[0].portName,
    route:data[0].route,
    jsonData:data[0].jsonData
  })
})







router.post('/ajax/postData', function*(next) {
  var form = this.request.body;
  console.log(99999)
  console.log(form)
  var self = this;
  var data=null;
 data = yield Service.postData(form);
 console.log(data)
 if(data)
 {this.body = this.render(200, data);
 }
 else{
   console.log(data+'失败了')
   data='false'
   this.body = this.render(200, data);
 }
});
router.post('/ajax/modifyData', function*(next) {
  var form = this.request.body;
  // console.log(form)
  var self = this;
  var data = yield Service.modifyData(form);
  console.log(data)
  if(data)
  {this.body = this.render(200, data);
  }
  else{
    console.log(data+'失败了')
    data='false'
    this.body = this.render(200, data);
  }
});


// router.get('/api/:route',function*(next){
//   var self=this;
//   var param=self.params;
//   console.log(param);
//   // console.log(self.query);
//   var data=yield Service.saveData(param.route);
//   this.body=this.render(200,data);
// })
